﻿/*
   Copyright 2013 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Flower.Client;
using Flower.Directory.Host.Default;
using Flower.Services.Data;
using Flower.Testing;
using NUnit.Framework;
using System.Threading;

namespace Flower.Logging.Tests
{
    [TestFixture]
    public class DirectoryLoggingTests
    {
        [Test]
        public void LoggingToDirectory()
        {
            var client = (IFlowerClient)LogManager.AppContext.GetObject("flowerClient");

            new DirectoryBuilder(client.Directory)
                .Root("/Sets/Local")
                    .Folder("#123")
                        .Set("Log", typeof(string).AssemblyQualifiedName, 0).End()
                    .End()
                .End();

            var logProcess = LogManager.CreateLog("Process#123");
            var log = LogManager.CreateLog("TestLog");

            for (int i = 0; i < 11; i++)
            {
                log.Trace("This message must not be put to the directory.");
            }
            
            LogManager.Flush();

            Assert.AreEqual(0, client.Directory.CountChildren("/Sets/Local/#123/Log", DirectoryEntryTypes.Message));

            for (int i = 0; i < 10; i++)
            {
                logProcess.Trace("This message must be put to the directory.");
            }

            LogManager.Flush();

            int limit = 10;

            do
            {
                Thread.Sleep(1000);
            }
            while (client.Directory.CountChildren("/Sets/Local/#123/Log", DirectoryEntryTypes.Message) < 10 && limit-- > 0);

            Assert.AreEqual(10, client.Directory.CountChildren("/Sets/Local/#123/Log", DirectoryEntryTypes.Message));
        }
    }
}
